package com.sczmgk.syevaluation.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.sczmgk.syevaluation.activity.LoginActivity;
import com.sczmgk.syevaluation.entity.output.UserMetter;
import com.sczmgk.syevaluation.entity.output.UserMetterBean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@SuppressLint("WorldReadableFiles")
public class SpfUtil {

    public static final int USER_CUSTOMER = 0;
    public static final int USER_STAFF = 1;
    public static final String LAST_USERNAME = "last_username";
    public static final String CACHE_FILE_NAME = "sy_evaluate_cache";
    public static String NAME_KEY = "name_key";
    public static String WORKNUM_KEY = "worknum_key";
    public static String WINDOWNUM_KEY = "windownum_key";
    public static String HEADPATH_KEY = "headpath_key";
    public static String DEPARTMENT_KEY = "department_key";
    public static String METTERS_KEY = "metters_key";
    public static String WINDOWNAME_KEY = "windowname_key";
    public static String PUSHTIME_KEY = "pushtime_key";
    public static String LOGIN_STATUS ="login_status";
    public static String USER_TYPE_KEY = "user_type_key";
     /**
     * 设置SharePreference文件中的字段的值
     * 
     * @param ctx 上下文
     * @param key 字段
     * @param value 值
     */
    @SuppressLint("WorldWriteableFiles")
    public static boolean setValue(Context ctx, String key, Object value) {
        boolean status = false;
        SharedPreferences spf = null;
        spf = ctx.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        String type = value.getClass().getSimpleName();// 获取数据类型
        SharedPreferences.Editor editor = spf.edit();
        if (spf != null) {
            if ("String".equals(type)) {
                editor.putString(key, (String) value);
            } else if ("Integer".equals(type)) {
                editor.putInt(key, (Integer) value);
            } else if ("Boolean".equals(type)) {
                editor.putBoolean(key, (Boolean) value);
            } else if ("Long".equals(type)) {
                editor.putLong(key, (Long) value);
            } else if ("Float".equals(type)) {
                editor.putFloat(key, (Float) value);
            }
            status = editor.commit();
        }
        return status;
    }

    /**
     * 获得SharePreference的值
     * 
     * @param ctx 上下文
     * @param key 字段
     * @param defValue 默认值
     * @return 获得对应key的值
     */
    @SuppressLint("WorldWriteableFiles")
    public static Object getValue(Context ctx, String key, Object defValue) {
        SharedPreferences spf = null;
        spf = ctx.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        String type = defValue.getClass().getSimpleName();// 获取数据类型
        if (spf != null) {
            if (type.equals("String")) {
                return spf.getString(key, (String) defValue);
            } else if (type.equals("Integer")) {
                return spf.getInt(key, (Integer) defValue);
            } else if (type.equals("Boolean")) {
                return spf.getBoolean(key, (Boolean) defValue);
            } else if (type.equals("Long")) {
                return spf.getLong(key, (Long) defValue);
            } else if (type.equals("Float")) {
                return spf.getFloat(key, (Float) defValue);
            }
        }
        return null;
    }

    /**
     * 将所有SharedPreferences文件删除
     */
    public static void clearAllShare(Context ctx) {

    }

    /**
     * 删除名字 为 fileNames 的 SharedPreferences文件
     * 
     * @param ctx
     * @param fileName
     * @return
     */
    @SuppressLint("SdCardPath")
    public static boolean delShare(Context ctx, String fileName) {

        boolean delstate = false;
        File file = new File("/data/data/" + ctx.getPackageName().toString() + "/shared_prefs", fileName);
        if (file.exists()) {
            delstate = file.delete();
        }
        return delstate;
    }

    /**
     * 将SharedPreferences转化成xml格式的字符串
     * 
     * @param spName SharedPreferences文件的名字
     * @return xml根式的字符串
     */

    @SuppressLint("SdCardPath")
    public static String SharedPreferencesTransfromXmlString(String spName) {
        String path = "/data/data/com.pingan.mobilewallet/shared_prefs/" + spName + ".xml";
        File file = new File(path);
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            if (fileInputStream != null) {
                BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                StringBuffer tStringBuffer = new StringBuffer();
                String sTempOneLine = new String("");
                while ((sTempOneLine = tBufferedReader.readLine()) != null) {
                    tStringBuffer.append(sTempOneLine);
                }
                return tStringBuffer.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }




    /**
     * 是否登录
     * @param context
     * @return
     */
   public static boolean isLogin(Context context){
        return (boolean) getValue(context,LOGIN_STATUS,false);
   }

    /**
     * 设置登录状态
     * @param context
     * @param isLogin
     */
   public static void  setLoginStatus(Context context,boolean isLogin){
       setValue(context,LOGIN_STATUS,isLogin);
   }




    public static void setName(Context context,String name){
        setValue(context,NAME_KEY,name);
    }
    public static String getName(Context context){
        return (String) getValue(context,NAME_KEY,"");
    }
    public static void  setWorknum(Context context ,String worknum){
        setValue(context,WORKNUM_KEY,worknum);
    }
    public static String getWorknum(Context context){
        return (String)getValue(context,WORKNUM_KEY,"");
    }
    public static void setWindownum(Context context,String windownum){
        setValue(context,WINDOWNUM_KEY,windownum);
    }
    public static String getWindownum(Context context){
        return (String) getValue(context,WINDOWNUM_KEY,"");
    }
    public static void setHeadPath(Context context,String headPath){

        setValue(context,HEADPATH_KEY,headPath);
    }
    public static String getHeadPath(Context context){
        return (String) getValue(context,HEADPATH_KEY,"");
    }
    public static void setDepartment(Context conext ,String deaprtment){
        setValue(conext,DEPARTMENT_KEY,deaprtment);
    }
    public static String getDepartment(Context context){
        return (String)getValue(context,DEPARTMENT_KEY,"");
    }
    public static void setMetters(Context context,String metters){
        setValue(context,METTERS_KEY,metters);
    }
    public static String getMetters(Context context){
        return (String)getValue(context,METTERS_KEY,"");
    }
    public static void setWindowName(Context context,String windowName){
        setValue(context,WINDOWNAME_KEY,windowName);
    }
    public static String getWindowName(Context context){
        return (String)getValue(context,WINDOWNAME_KEY,"");
    }
    public static void setPushTime(Context context,int pushTime){
        setValue(context,PUSHTIME_KEY,pushTime);
    }
    public static int getPushTime(Context context){
        return (int)getValue(context,PUSHTIME_KEY,10);
    }

    public static void setUserType(Context context,int type){
       setValue(context,USER_TYPE_KEY,type);
    }
    public static int getUserType(Context context){
       return (int)getValue(context,USER_TYPE_KEY,USER_CUSTOMER);
    }
    public static  void saveLoginInfo(Context context,UserMetter userMetter){
        UserMetterBean data = userMetter.data;
        SpfUtil.setDepartment(context,data.department);
        SpfUtil.setHeadPath(context,data.headpath);
        SpfUtil.setMetters(context,data.metters);
        SpfUtil.setLoginStatus(context,true);
        SpfUtil.setName(context,data.name);
        SpfUtil.setPushTime(context,data.pushtime);
        SpfUtil.setWindowName(context,data.windowname);
        SpfUtil.setWindownum(context,data.windownum);
        SpfUtil.setWorknum(context,data.worknum);
    }

    public static UserMetterBean getLoginInfo(Context context) {
        UserMetterBean userMetterBean  = new UserMetterBean();
        userMetterBean.department = SpfUtil.getDepartment(context);
        userMetterBean.headpath = SpfUtil.getHeadPath(context);
        userMetterBean.metters = SpfUtil.getMetters(context);
        userMetterBean.name = SpfUtil.getName(context);
        userMetterBean.pushtime = SpfUtil.getPushTime(context);
        userMetterBean.windowname = SpfUtil.getWindowName(context);
        userMetterBean.windownum = SpfUtil.getWindownum(context);
        userMetterBean.worknum = SpfUtil.getWorknum(context);
        return  userMetterBean;
    }

}
